home *** CD-ROM | disk | FTP | other *** search
/ Ahoy 1985 June / Ahoy_Magazine_85-06_1985_Double_L.d64 / towers of hanoi (.txt) < prev    next >
Commodore BASIC  |  2022-10-26  |  8KB  |  260 lines

  1. 10 rem - towers of hanoi for commodore 64
  2. 20 rem - daniel miller
  3. 30 rem - 2815 34 street
  4. 40 rem - astoria, queens
  5. 50 rem - new york, new york   11103
  6. 60 rem - september 1, 1983
  7. 70 rem ********************************
  8. 80 goto2150
  9. 90 poke53281,1:zz=1:printcl$;left$(do$,2);tab(11);hdg$;
  10. 100 poke55587,10:poke1315,49:poke55595,10:poke1323,50:poke55603,10:poke1331,51
  11. 110 print left$(do$,20);tab(5);a$(1);left$(do$,9);tab(11);a$(3);
  12. 120 printleft$(do$,9);tab(19);a$(3);left$(do$,9);tab(27);a$(3)
  13. 130 rem - place sprites on tower #1
  14. 140 pokev+14,93:pokev+15,160:pokev+12,93:pokev+13,151:pokev+10,93:pokev+11,142
  15. 150 pokev+8,93:pokev+9,133:pokev+6,93:pokev+7,124:pokev+4,93:pokev+5,115
  16. 160 pokev+2,93:pokev+3,106:pokev+0,93:pokev+1,97
  17. 170 if yn$="y" thenn2=8-num:goto200
  18. 180 if zz=0 then350
  19. 190 printleft$(do$,22);tab(8);num$;:input num:n2=8-num:sp=0:if num=8 then270
  20. 200 if num<2 or num>8 thenprint left$(do$,22);tab(5);sp$;:goto190
  21. 210 if num=8 and yn$="y" then270
  22. 220 n4=n2
  23. 230 for x=1 to n4
  24. 240 sp=sp+(2^(n4-1))
  25. 250 n4=n4-1:next x
  26. 260 pokev+21,(255-sp)
  27. 270 zz=0:for tm=1 to 250:next tm:printleft$(do$,22);tab(5);sp$;
  28. 280 t1$="":t2$="":t3$="":n3=n2+1
  29. 290 for x=1 to num:t1$=t1$+t4$(x):t2$=t2$+t5$(x):t3$=t3$+t6$(x):next x
  30. 300 if yn$="y" then1430
  31. 310 for x=1 to num:y=2^x-1:next x
  32. 320 num$=rv$+g2$+str$(num)+ro$+slv$(1)+rv$+g2$+str$(y)+slv$(2):y0=y
  33. 330 printleft$(do$,22);tab(6);num$;:for tm=1 to 2000:next tm
  34. 340 for tm=1 to 250:next tm:printleft$(do$,22);tab(5);sp$;
  35. 350 printleft$(do$,22);tab(6);f9$;"    ";tab(24);t9$;"    ";
  36. 360 printleft$(do$,22);tab(6);f9$;" ";cr$;
  37. 370 num$="":slv$(1)="":slv$(2)=""
  38. 380 if za=2 then420
  39. 390 get twr$(1):if twr$(1)="" then390
  40. 400 if twr$(1)="[133]" then1330:rem - 'f1'
  41. 410 if (twr$(1)<"1" or twr$(1)>"3") thentwr$(1)="":goto390
  42. 420 print rv$+red$+twr$(1)+ro$;
  43. 430 gosub520
  44. 440 printleft$(do$,22);tab(34);" ";cr$;
  45. 450 if za=2 then480
  46. 460 get twr$(2):if twr$(2)="" then460
  47. 470 if twr$(2)<"1" or twr$(2)>"3" thentwr$(2)="":goto460
  48. 480 print rv$+red$+twr$(2)+ro$;
  49. 490 if twr$(1)<>twr$(2) then510
  50. 500 er$=rv$+red$+" invalid entries"+ro$:goto610
  51. 510 goto640
  52. 520 x=1:gosub530:return
  53. 530 on val(twr$(x)) goto540,560,580
  54. 540 if val(t1$)=0 then600
  55. 550 return
  56. 560 if val(t2$)=0 then600
  57. 570 return
  58. 580 if val(t3$)=0 then600
  59. 590 return
  60. 600 er$=rv$+red$+"tower # "+twr$(x)+" is empty"+ro$
  61. 610 for y=1 to 5:printleft$(do$,24);tab(12)er$;:for tm=1 to 250:next tm
  62. 620 printleft$(do$,24);tab(12);left$(sp$,18);:for tm=1 to 250
  63. 630 next tm,y:er$="":goto350
  64. 640 on val(twr$(1)) goto650,680,710
  65. 650 for x=1 to num:if mid$(t1$,x,1)="1" then670
  66. 660 next x:loc(1)=160:goto740
  67. 670 loc(1)=t1(n2+x):t4$(n2+x)="0":goto740
  68. 680 for x=1 to num:if mid$(t2$,x,1)="1" then700
  69. 690 next x:loc(1)=160:goto740
  70. 700 loc(1)=t2(n2+x):t5$(n2+x)="0":goto740
  71. 710 for x=1 to num:if mid$(t3$,x,1)="1" then730
  72. 720 next x:loc(1)=160:goto740
  73. 730 loc(1)=t3(n2+x):t6$(n2+x)="0"
  74. 740 lc=loc(1):gosub880:sp(1)=sp
  75. 750 on val(twr$(2)) goto760,790,820
  76. 760 for x=1 to num:if mid$(t1$,x,1)="1" then780
  77. 770 next x:loc(2)=160:t4$(n2+x-1)="1":goto850
  78. 780 loc(2)=t1(n2+x-1):t4$(n2+x-1)="1":goto850
  79. 790 for x=1 to num:if mid$(t2$,x,1)="1" then810
  80. 800 next x:loc(2)=160:t5$(n2+x-1)="1":goto850
  81. 810 loc(2)=t2(n2+x-1):t5$(n2+x-1)="1":goto850
  82. 820 for x=1 to num:if mid$(t3$,x,1)="1" then840
  83. 830 next x:loc(2)=160:t6$(n2+x-1)="1":goto850
  84. 840 loc(2)=t3(n2+x-1):t6$(n2+x-1)="1"
  85. 850 lc=loc(2):gosub880:sp(2)=sp
  86. 860 t1$="":t2$="":t3$="":n5=n2+1
  87. 870 for x=n5 to 8:t1$=t1$+t4$(x):t2$=t2$+t5$(x):t3$=t3$+t6$(x):next x:goto960
  88. 880 if lc=97 thensp=1:return
  89. 890 if lc=106 thensp=2:return
  90. 900 if lc=115 thensp=3:return
  91. 910 if lc=124 thensp=4:return
  92. 920 if lc=133 thensp=5:return
  93. 930 if lc=142 thensp=6:return
  94. 940 if lc=151 thensp=7:return
  95. 950 if lc=160 thensp=8:return
  96. 960 on val(twr$(1)) goto970,980,990
  97. 970 sq%=r1%(1,sp(1)):r1%(1,sp(1))=8:goto1000
  98. 980 sq%=r2%(1,sp(1)):r2%(1,sp(1))=8:goto1000
  99. 990 sq%=r3%(1,sp(1)):r3%(1,sp(1))=8
  100. 1000 on val(twr$(2)) goto1010,1020,1030
  101. 1010 r1%(1,sp(2))=sq%:goto1040
  102. 1020 r2%(1,sp(2))=sq%:goto1040
  103. 1030 r3%(1,sp(2))=sq%
  104. 1040 on val(twr$(1)) goto1050,1060,1070
  105. 1050 x1=93:goto1080
  106. 1060 x1=157:goto1080
  107. 1070 x1=222
  108. 1080 on val(twr$(2)) goto1090,1100,1110
  109. 1090 x2=93:goto1120
  110. 1100 x2=157:goto1120
  111. 1110 x2=222
  112. 1120 rem - move sprite
  113. 1130 on sq%+1 goto1140,1150,1160,1170,1180,1190,1200,1210
  114. 1140 sp=0:sq=1:goto1220
  115. 1150 sp=2:sq=3:goto1220
  116. 1160 sp=4:sq=5:goto1220
  117. 1170 sp=6:sq=7:goto1220
  118. 1180 sp=8:sq=9:goto1220
  119. 1190 sp=10:sq=11:goto1220
  120. 1200 sp=12:sq=13:goto1220
  121. 1210 sp=14:sq=15
  122. 1220 if val(twr$(1))>val(twr$(2)) then1260
  123. 1230 for yy=loc(1) to 55 step-1:poke v+sp,x1:poke v+sq,yy:next yy
  124. 1240 for xx=x1 to x2:poke v+sp,xx:poke v+sq,55:next xx
  125. 1250 for yy=55 to loc(2):poke v+sp,x2:poke v+sq,yy:next yy:goto1290
  126. 1260 for yy=loc(1) to 55 step-1:poke v+sp,x1:poke v+sq,yy:next yy
  127. 1270 for xx=x1 to x2 step-1:poke v+sp,xx:poke v+sq,55:next xx
  128. 1280 for yy=55 to loc(2):pokev+sp,x2:pokev+sq,yy:next yy
  129. 1290 mv=mv+1:printleft$(do$,24);tab(17);mv$+rv$+b2$+str$(mv)+ro$;
  130. 1300 if za=2 thenmw=mw+1:goto1440:rem - computer's next move
  131. 1310 goto350:rem - player's next move
  132. 1320 rem - computer solution
  133. 1330 twr$(1)="":printleft$(do$,24);tab(7);cmp$;
  134. 1340 get yn$:if yn$="" then1340
  135. 1350 if yn$="y" then1370
  136. 1360 if yn$<>"n" thenyn$="":goto1330
  137. 1370 print rv$+bl$+" "+yn$+ro$;:for tm=1 to 500:next tm:if yn$="y" then1420
  138. 1380 for tm=1 to 1000:next tm
  139. 1390 printleft$(do$,24);tab(7);sp$;left$(do$,24);tab(9);en$;
  140. 1400 get rt$:if rt$<>chr$(13) then1400
  141. 1410 poke v+21,0:print cl$;:end
  142. 1420 sp=0:mv=0:mw=1:goto1990
  143. 1430 yn$="":za=2:printleft$(do$,24);tab(7);sp$;
  144. 1440 bi$="":for x=1 to 8:bi$(x)="0":next x
  145. 1450 if mv=y0 then1380
  146. 1460 ct=fre(0)
  147. 1470 c2=0:c3=0:c4=0:c5=0:c6=0:za=2
  148. 1480 dec=mw
  149. 1490 for y=0 to 8:if int(2^y)>dec theny=y-1:bi$(y)="1":goto1510
  150. 1500 bi$(y)="0":next y
  151. 1510 dec=dec-2^y:if dec=0 then1530
  152. 1520 goto1490
  153. 1530 for y=7 to 0 step-1:bi$=bi$+bi$(y):next y
  154. 1540 for x=8 to 1 step-1:if mid$(bi$,x,1)="0" thenc2=c2+1:next x
  155. 1550 c2=c2+1
  156. 1560 ri%=c2+num-1:on num-1 goto1570,1580,1590,1600,1610,1620,1630
  157. 1570 ri%=ri%+4:goto1640
  158. 1580 ri%=ri%+2:goto1640
  159. 1590 goto1640
  160. 1600 ri%=ri%-2:goto1640
  161. 1610 ri%=ri%-4:goto1640
  162. 1620 ri%=ri%-6:goto1640
  163. 1630 ri%=abs(ri%-8)
  164. 1640 for x=1 to 8:if r1%(1,x)=ri% thentwr$(1)="1":goto1670
  165. 1650 next x:for x=1 to 8:if r2%(1,x)=ri% thentwr$(1)="2":goto1670
  166. 1660 next x:twr$(1)="3"
  167. 1670 l$=twr$(1)
  168. 1680 for x=1 to 8:if mid$(bi$,x,1)="1" thenc3=c3+1:next x:goto1700
  169. 1690 next x
  170. 1700 if c3>1 then1740
  171. 1710 if val(t1$)=0 thentwr$(2)="1":goto350
  172. 1720 if val(t2$)=0 thentwr$(2)="2":goto350
  173. 1730 if val(t3$)=0 thentwr$(2)="3":goto350
  174. 1740 for x=8 to 1 step-1:c4=c4+1:if mid$(bi$,x,1)="0" thennext x
  175. 1750 c6=c4
  176. 1760 for x=8-c4 to 1 step-1:c6=c6+1:if mid$(bi$,x,1)="0"thenc5=c5+1:next x
  177. 1770 rj%=c6+num-1:on num-1 goto1780,1790,1800,1810,1820,1830,1840
  178. 1780 rj%=rj%+4:goto1850
  179. 1790 rj%=rj%+2:goto1850
  180. 1800 goto1850
  181. 1810 rj%=rj%-2:goto1850
  182. 1820 rj%=rj%-4:goto1850
  183. 1830 rj%=rj%-6:goto1850
  184. 1840 rj%=abs(rj%-8)
  185. 1850 for x=1 to 8:if r1%(1,x)=rj% thentx$="1":goto1880
  186. 1860 next x:for x=1 to 8:if r2%(1,x)=rj% thentx$="2":goto1880
  187. 1870 next x:tx$="3"
  188. 1880 m$=tx$
  189. 1890 lm$=l$+m$
  190. 1900 if (c5/2)=int(c5/2) or c5=0 thentwr$(2)=tx$:goto1970
  191. 1910 if lm$="12" thentwr$(2)="3":goto1970
  192. 1920 if lm$="13" thentwr$(2)="2":goto1970
  193. 1930 if lm$="23" thentwr$(2)="1":goto1970
  194. 1940 if lm$="21" thentwr$(2)="3":goto1970
  195. 1950 if lm$="31" thentwr$(2)="2":goto1970
  196. 1960 if lm$="32" thentwr$(2)="1"
  197. 1970 l$="":m$="":lm$="":ct=fre(0):goto350
  198. 1980 rem - build sprites
  199. 1990 v=53248:pokev+21,255:pokev+23,255:pokev+29,255
  200. 2000 poke2040,192:poke2041,193:poke2042,194:poke2043,195:poke2044,196
  201. 2010 poke2045,197:poke2046,198:poke2047,199
  202. 2020 pokev+39,3:pokev+40,15:pokev+41,13:pokev+42,9:pokev+43,14:pokev+44,5
  203. 2030 poke v+45,8:pokev+46,0
  204. 2040 restore:n0=12288:for x=1 to 8:for n=0 to 62:read q:poke n0+n,q:next n
  205. 2050 n0=n0+64:next x
  206. 2060 t1$="11111111":t2$="00000000":t3$="00000000"
  207. 2070 for x=1 to 8:t4$(x)=mid$(t1$,x,1):next x
  208. 2080 for x=1 to 8:t5$(x)=mid$(t2$,x,1):next x
  209. 2090 for x=1 to 8:t6$(x)=mid$(t3$,x,1):next x
  210. 2100 z%=97:for x=1 to 8:t1(x)=z%:t2(x)=z%:t3(x)=z%:z%=z%+9:next x
  211. 2110 y%=0:for x=1 to 8:r1%(1,x)=y%:y%=y%+1:next x
  212. 2120 for x=1 to 8:r2%(1,x)=9:r3%(1,x)=9:next x
  213. 2130 if yn$="y" then140
  214. 2140 goto90
  215. 2150 rem - build character graphics
  216. 2160 blk$=chr$(144):red$=chr$(28):yel$=chr$(158):b2lu$=chr$(31):cy$=chr$(159)
  217. 2170 g1rey$="[151]":g2rey$="[152]":rem - c=(4); c=(5)
  218. 2180 rv$=chr$(18):ro$=chr$(146)
  219. 2190 cl$=chr$(147):dw$=chr$(17):lf$=chr$(157)
  220. 2200 for x=1 to 24:do$=do$+dw$:next x:do$=chr$(19)+do$
  221. 2210 a$(1)=rv$+b2$+"                              "+ro$
  222. 2220 a$(2)=rv$+yel$+" "+dw$+lf$
  223. 2230 for x=1 to 10:a$(3)=a$(3)+a$(2):next x
  224. 2240 a$(3)=a$(3)+" "+ro$
  225. 2250 hdg$=rv$+wh$+"towers of hanoi"+ro$
  226. 2260 num$=rv$+b2$+"number of rings (2 - 8)"+ro$
  227. 2270 sp$="                              "
  228. 2280 f9$=rv$+g1$+"from tower #"+ro$:t9$=rv$+g1$+"to tower #"+ro$
  229. 2290 cr$=rv$+cy$+" "+ro$
  230. 2300 slv$(1)=rv$+g2$+" rings requires"+ro$:slv$(2)=rv$+g2$+" moves."+ro$
  231. 2310 mv$=rv$+b2$+"move #"+ro$
  232. 2320 cmp$=rv$+g1$+"computer solution (y/n) ?"+ro$
  233. 2330 en$=rv$+g1$+"press <return> to end"+ro$
  234. 2340 goto1990
  235. 2350 rem - data for sprites
  236. 2360 rem - sprite 0
  237. 2370 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,:rem 53 commas
  238. 2380 data 1,255,128,1,255,128,1,255,128
  239. 2390 rem - sprite 1
  240. 2400 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  241. 2410 data 3,255,192,3,255,192,3,255,192
  242. 2420 rem - sprite 2
  243. 2430 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  244. 2440 data 7,255,224,7,255,224,7,255,224
  245. 2450 rem - sprite 3
  246. 2460 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  247. 2470 data 15,255,240,15,255,240,15,255,240
  248. 2480 rem - sprite 4
  249. 2490 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  250. 2500 data 31,255,248,31,255,248,31,255,248
  251. 2510 rem - sprite 5
  252. 2520 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  253. 2530 data 63,255,252,63,255,252,63,255,252
  254. 2540 rem - sprite 6
  255. 2550 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  256. 2560 data 127,255,254,127,255,254,127,255,254
  257. 2570 rem - sprite 7
  258. 2580 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  259. 2590 data 255,255,255,255,255,255,255,255,255
  260.